package cn.goktech.sports.modules.importDataone.controller;

import com.sun.media.jfxmedia.logging.Logger;
import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.util.ClassUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;

/**
 * 〈一句话功能简述〉<br>
 * 〈下载资源〉
 *
 * @author PanY
 * @create 2019/11/6
 * @since 1.0.0
 */
@Controller
@Slf4j
public class ResourceController {

    @ResponseBody
    @RequestMapping( value = "/download",method = {RequestMethod.POST})
    public String download(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String fileName = "模板.rar";
        log.info(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>进入下载驱动");
        response.setCharacterEncoding("utf-8");
        //返回的数据类型
        response.setContentType("application/octet-stream");
        //响应头
        response.setHeader("Content-Disposition", "attachment;fileName="
                + URLEncoder.encode(fileName, "UTF-8"));
        InputStream inputStream=null;
        OutputStream outputStream=null;
        String realPath = request.getSession().getServletContext().getRealPath(File.separator);
        realPath = realPath+java.io.File.separator+"static"+java.io.File.separator+"";
        realPath = "/home/";
        log.info(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>文件完整路径：{}", realPath +fileName);
        String jar_parent = new File(ResourceUtils.getURL("classpath:").getPath()).getParentFile().getParentFile().getParent();
        jar_parent +=java.io.File.separator+"emsyingxiaoguanlixitong"+java.io.File.separator + "src" + java.io.File.separator + "main" + java.io.File.separator +
                "webapp" +java.io.File.separator+"static"+java.io.File.separator+"模板.rar";
        System.out.println("第一种：" + jar_parent);
        String path2 = ClassUtils.getDefaultClassLoader().getResource("").getPath();
        System.out.println("第三种：" + path2+java.io.File.separator+"static"+java.io.File.separator + fileName);
        String path3 = ResourceUtils.getURL("classpath:").getPath();
        System.out.println("第四种：" + path3+java.io.File.separator+"static"+java.io.File.separator + fileName);
        byte[] bytes = new byte[2048];
        try {
            //文件
            File file=new File(realPath,fileName);
            if ( !file.isFile() ){//判断是否为文件,如果不是，或者没有，则错误
                log.info(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>文件路径不是文件：错误！");//打印路径
                return "文件名称错误";
            }
            log.info(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>文件：{}", file.getName());
            inputStream = new FileInputStream(file);
            outputStream = response.getOutputStream();
            int length;
            //inputStream.read(bytes)从file中读取数据,-1是读取完的标志
            while ((length = inputStream.read(bytes)) > 0) {
                //写数据
                outputStream.write(bytes, 0, length);
            }
            outputStream.flush();
        } catch (FileNotFoundException e) {
            log.info(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>没有找到文件");
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //关闭输入输出流
            if(outputStream!=null) {
                outputStream.close();
            }
            if(inputStream!=null) {
                inputStream.close();
            }
        }
        return null;
    }
}
